package com.xipu.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xipu.yygh.common.exception.YyghException;
import com.xipu.yygh.enums.OrderStatusEnum;
import com.xipu.yygh.enums.PaymentStatusEnum;
import com.xipu.yygh.model.order.OrderInfo;
import com.xipu.yygh.model.order.PaymentInfo;
import com.xipu.yygh.order.mapper.OrderInfoMapper;
import com.xipu.yygh.order.mapper.PaymentInfoMapper;
import com.xipu.yygh.order.service.PaymentInfoService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 支付信息表 服务实现类
 * </p>
 *
 * @author gql
 * @since 2022-05-14
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    /**
     * 订单信息
     */
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    public void savePaymentInfo(OrderInfo order, Integer paymentType) {
        // 1.查询支付记录是否存在
        Integer count = this.baseMapper.selectCount(Wrappers.lambdaQuery(PaymentInfo.class)
                .eq(PaymentInfo::getOrderId, order.getId())
                .eq(PaymentInfo::getPaymentType, paymentType));
        // 2.如果存在就不需要再添加
        if (count > 0) {
            return;
        }
        // 2.如果不存在,就添加支付记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(order.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(order.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(order.getReserveDate()).toString("yyyy-MM-dd") + "|" + order.getHosname() + "|" + order.getDepname() + "|" + order.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(order.getAmount());
        this.baseMapper.insert(paymentInfo);
    }

    @Override
    public void paySuccess(String outTradeNo, Integer paymentType, Map<String, String> paramMap) {
        //1.更新订单表的状态为已支付
        OrderInfo orderInfo = this.orderInfoMapper.selectOne(Wrappers.lambdaQuery(OrderInfo.class)
                .eq(OrderInfo::getOutTradeNo, outTradeNo));
        if (ObjectUtils.isEmpty(orderInfo)) {
            throw new YyghException(20001, "根据此交易号无法查到订单信息");
        }
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        this.orderInfoMapper.updateById(orderInfo);

        //2.更新支付记录表的状态为已支付
        PaymentInfo paymentInfo = this.baseMapper.selectOne(Wrappers.lambdaQuery(PaymentInfo.class)
                .eq(PaymentInfo::getOutTradeNo, outTradeNo));
        if (ObjectUtils.isEmpty(paymentInfo)) {
            throw new YyghException(20001, "根据此交易号无法查到支付信息");

        }
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        // 事务id,由微信支付状态接口返回,退款时使用
        paymentInfo.setTradeNo(paramMap.get("transaction_id"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramMap.toString());
        this.baseMapper.updateById(paymentInfo);
    }
}
